{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#LAS\n",
    "from sklearn.decomposition import TruncatedSVD # 导入截断奇异值分解模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.         0.85065081]\n",
      " [0.         0.85065081]\n",
      " [0.         1.37638192]\n",
      " [0.         0.52573111]\n",
      " [1.41421356 0.        ]\n",
      " [0.70710678 0.        ]\n",
      " [1.41421356 0.        ]\n",
      " [0.70710678 0.        ]]\n",
      "[0.38596491 0.27999429]\n",
      "0.6659592065833294\n"
     ]
    }
   ],
   "source": [
    "data =  [[1, 0, 0, 0],\n",
    "        [1, 0, 0, 0],\n",
    "        [1, 1, 0, 0],\n",
    "        [0, 1, 0, 0],\n",
    "        [0, 0, 1, 1],\n",
    "        [0, 0, 1, 0],\n",
    "        [0, 0, 1, 1],\n",
    "        [0, 0, 0, 1]]\n",
    "n_components = 2 # 设置主成分的数量\n",
    "model = TruncatedSVD(n_components=n_components) # 创建截断奇异值分\n",
    "model = model.fit(data) # 拟合数据\n",
    "print(model.transform(data))  # 变换后的数据\n",
    "print(model.explained_variance_ratio_)  # 贡献率\n",
    "print(sum(model.explained_variance_ratio_))  # 累计贡献率"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ml",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
